﻿Imports StedySoft.SenseSDK

Public Class frmSetup
    Dim WithEvents objSep1 As New SensePanelDividerItem
    Dim WithEvents objUnit As New SensePanelComboItem
    Dim WithEvents objCurrency As New SensePanelTextboxItem
    Dim WithEvents objSoundLanguage As New SensePanelComboItem
    Dim WithEvents objSep2 As New SensePanelDividerItem
    Dim WithEvents objLitrePer100KM As New SensePanelTextboxItem
    Dim WithEvents objPriceOf20Litres As New SensePanelTextboxItem
    Dim WithEvents objSep3 As New SensePanelDividerItem
    Dim WithEvents objEnableAlerts As New SensePanelOnOffItem
    Dim WithEvents objSpeedAlert1 As New SensePanelNumericItem
    Dim WithEvents objSpeedAlert2 As New SensePanelNumericItem

    Private Sub frmSetup_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If lstMain.Count = 0 Then
            objSep1.Text = "Regional Settings"
            objUnit.LabelText = "Unit"
            objUnit.Items.Add(New SensePanelComboItem.Item With {.Text = "Metric"})
            objUnit.Items.Add(New SensePanelComboItem.Item With {.Text = "Imperial"})
            objUnit.SelectedText = Glob.MobileSettings.Unit
            objCurrency.LabelText = "Currency"
            objCurrency.Text = Glob.MobileSettings.Currency

            objSoundLanguage.LabelText = "Sound Language"
            objSoundLanguage.Items.AddRange(GetLangs)
            objSoundLanguage.SelectedText = Glob.MobileSettings.SoundLanguage

            objSep2.Text = "Fuel Consumption"
            objLitrePer100KM.LabelText = "Litter Per 100 KM"
            objLitrePer100KM.Text = Glob.MobileSettings.LitrePer100KM
            objPriceOf20Litres.LabelText = "Price Of 20 Litres"
            objPriceOf20Litres.Text = Glob.MobileSettings.PriceOf20Litres
            objSep3.Text = "Alert Manager"
            objEnableAlerts.ButtonAnimation = True
            objEnableAlerts.Status = If(Glob.MobileSettings.EnableAlerts, SensePanelOnOffItem.ItemStatus.On, SensePanelOnOffItem.ItemStatus.Off)
            objEnableAlerts.PrimaryText = "Enable Speed Limit Alerts"

            objSpeedAlert1.PrimaryText = "First Speed Limit"
            objSpeedAlert1.Value = Glob.MobileSettings.SpeedAlert
            objSpeedAlert2.PrimaryText = "Second Speed Limit"
            objSpeedAlert2.Value = Glob.MobileSettings.SpeedAlert2
            objSpeedAlert1.Enabled = CBool(objEnableAlerts.Status = SensePanelOnOffItem.ItemStatus.On)
            objSpeedAlert2.Enabled = CBool(objEnableAlerts.Status = SensePanelOnOffItem.ItemStatus.On)

            lstMain.AddItem(objSep1)
            lstMain.AddItem(objUnit)
            lstMain.AddItem(objCurrency)
            lstMain.AddItem(objSoundLanguage)
            lstMain.AddItem(objSep2)
            lstMain.AddItem(objLitrePer100KM)
            lstMain.AddItem(objPriceOf20Litres)
            lstMain.AddItem(objSep3)
            lstMain.AddItem(objEnableAlerts)
            lstMain.AddItem(objSpeedAlert1)
            lstMain.AddItem(objSpeedAlert2)
        End If
    End Sub

    Private Function GetLangs() As List(Of SensePanelComboItem.Item)
        Dim SoundsPath As String = Glob.MobileSettings.AppPath
        SoundsPath = System.IO.Path.Combine(SoundsPath, "Sounds")
        Dim RtnVal = (From U In System.IO.Directory.GetDirectories(SoundsPath) _
                     Select New SensePanelComboItem.Item With {.Text = U.Replace(System.IO.Path.GetDirectoryName(U) & "\", "")}).ToList
        Return RtnVal
    End Function

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Glob.MobileSettings.Unit = objUnit.SelectedText
        Glob.MobileSettings.Currency = objCurrency.Text
        Glob.MobileSettings.SoundLanguage = objSoundLanguage.SelectedText

        Glob.MobileSettings.LitrePer100KM = CDbl(objLitrePer100KM.Text)
        Glob.MobileSettings.PriceOf20Litres = CDbl(objPriceOf20Litres.Text)

        Glob.MobileSettings.EnableAlerts = CBool(objEnableAlerts.Status = SensePanelOnOffItem.ItemStatus.On)
        Glob.MobileSettings.SpeedAlert = objSpeedAlert1.Value
        Glob.MobileSettings.SpeedAlert2 = objSpeedAlert2.Value

        Me.Close()
    End Sub

    Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
        Me.Close()
    End Sub

    Dim SIPOffset As Integer = 0

    Private Sub InputPanel1_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InputPanel1.EnabledChanged
        ReLayout()
    End Sub

    Private Sub ReLayout()
        Dim Hwnd = GetForegroundWindow()
        Dim StBuilder As New System.Text.StringBuilder
        StBuilder.Capacity = 9
        GetWindowText(Hwnd, StBuilder, StBuilder.Capacity)
        If StBuilder.ToString.ToLower = "frmsetup" Then
            If InputPanel1.Enabled Then
                Dim IItem As SenseListControl.ISenseListItem = lstMain.FocusedItem
                Dim R As Rectangle = IItem.ClientRectangle
                R.Offset(0, lstMain.Bounds.Top)
                If R.Bottom > InputPanel1.VisibleDesktop.Height Then
                    SIPOffset = Math.Abs(InputPanel1.VisibleDesktop.Height - R.Bottom)
                    lstMain.ScrollList(-SIPOffset)
                    lstMain.Invalidate()
                End If
            Else
                If SIPOffset <> 0 Then
                    lstMain.ScrollList(SIPOffset)
                    lstMain.Invalidate()
                    SIPOffset = 0
                End If
            End If
        End If
    End Sub

    Private Sub objCurrency_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles objCurrency.GotFocus
        InputPanel1.Enabled = True
        ReLayout()
    End Sub

    Private Sub objCurrency_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles objCurrency.LostFocus
        InputPanel1.Enabled = False
        ReLayout()
    End Sub

    Private Sub objLitrePer100KM_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles objLitrePer100KM.GotFocus
        Microsoft.WindowsCE.Forms.InputModeEditor.SetInputMode(objLitrePer100KM.Textbox, Microsoft.WindowsCE.Forms.InputMode.Numeric)
        InputPanel1.Enabled = True
        ReLayout()
    End Sub

    Private Sub objLitrePer100KM_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles objLitrePer100KM.LostFocus
        InputPanel1.Enabled = False
        ReLayout()
    End Sub

    Private Sub objPriceOf20Litres_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles objPriceOf20Litres.GotFocus
        InputPanel1.Enabled = True
        ReLayout()
    End Sub

    Private Sub objPriceOf20Litres_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles objPriceOf20Litres.LostFocus
        InputPanel1.Enabled = False
        ReLayout()
    End Sub

    Private Sub objEnableAlerts_OnStatusChanged(ByVal Sender As Object, ByVal Status As StedySoft.SenseSDK.SensePanelOnOffItem.ItemStatus) Handles objEnableAlerts.OnStatusChanged
        objSpeedAlert1.Enabled = CBool(objEnableAlerts.Status = SensePanelOnOffItem.ItemStatus.On)
        objSpeedAlert2.Enabled = CBool(objEnableAlerts.Status = SensePanelOnOffItem.ItemStatus.On)
    End Sub

End Class